package GUI;

import Enumeration.ChoosenDate;
import Enumeration.GUIButtons;
import Facade.Facade;
import Operations.Operation;
import Parser.ACreatorParser;
import com.toedter.calendar.JDateChooser;
import com.toedter.calendar.JMonthChooser;
import com.toedter.calendar.JYearChooser;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;

/**
 * @author Martin
 * @version 1.0
 * @created 14-III-2012 13:12:33
 */
public abstract class Subject extends AObserver implements ActionListener {

    private Operation operation;
    private Facade facade;
    protected ResultData resultData;
    protected JPanel hlavniPanel;
    protected JPanel operationFrame;
    protected JPanel resultFrame;
    protected JPanel pozadi;
    //protected JPanel pozadi = new JPanel();
    //nutne prvky pro operace
    protected JDateChooser[] datumy = new JDateChooser[2];
    protected JMonthChooser mesic = new JMonthChooser();
    protected JYearChooser rok = new JYearChooser();
    protected ChoosenDate vybranaMoznost = ChoosenDate.fromTo;

    public Subject(Facade facade) {
        this.facade = facade;
        hlavniPanel = new JPanel(new GridBagLayout());
        operationFrame = new JPanel(new GridBagLayout());
        resultFrame = new JPanel(new GridBagLayout());
        //pozadi = new JPanel(new GridBagLayout());
        resultData = new ResultData();
        operation = new Operation();
        operation.addObeserver(this); //pridame se jako posluchac nad operacemi
    }

    public void operationFrame() {
        change(operationFrame);
    }

    public void resultFrame() {
        change(resultFrame);
    }

    private void change(JPanel panel) {
        pozadi.setVisible(false);
        hlavniPanel.removeAll();
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1;
        c.insets = new Insets(10, 10, 10, 10);  //top padding
        hlavniPanel.add(panel, c);
        c.gridy=1;
        hlavniPanel.add(parserPanel, c);
        pozadi.revalidate();
        pozadi.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() instanceof JButton) {
            JButton tlacitko = (JButton) e.getSource();
            if (tlacitko.getName().equals(GUIButtons.zpet.getType())) {
                this.operationFrame();
            } else if (tlacitko.getName().equals(GUIButtons.export.getType())) {
                operation.export();
            } else if (tlacitko.getName().equals(GUIButtons.vypocet.getType())){
                //vypocet
                //umistit kontrolu datumu a volani adekvatniho operace
                //PRIDAT upozorneni pokud je datum spatne!!!! (staci dialogove okno)
                boolean validDate=false;
                
                switch(vybranaMoznost){
                    case fromTo:
                        //operation = new Operation(); neni potreba, subjekt uz si operation vytvari
                        if(isValidDate(datumy[0]) && isValidDate(datumy[1])){
                            if(datumy[1].getDate().getTime() - datumy[0].getDate().getTime()>0)
                            {                            
                                operation.operationAll(datumy[0].getDate(), datumy[1].getDate(), resultData);
                                validDate=true;}
                            else
                            {   messageInvalidDate("Prohozené datumy(datum do konce období ve kterém chcete spočítat data nastal dříve než jeho počátek.) ");        }

                        }
                        break;
                    case month:
                        //operation = new Operation();
                        operation.operationAll(mesic.getMonth(),rok.getYear(), resultData);
                        validDate=true;
                        break;
                    case all:
                        //operation = new Operation();
                        operation.operationAll(resultData);
                        validDate=true;
                        break;
                    default:
                }    
                
                if((validDate) && (resultData.getAverage()>=0))
                {
                    this.update();
                    this.resultFrame();
                }
                
            } else { 
                //defaultní možnost
            }
        }
    }
    protected void konec() {
        //okno.dispose();
        //facade.end(); //zatim zbytecne
        System.exit(0);

    }
    
    private boolean isValidDate(JDateChooser chooser)
    {
        if (chooser.getDate()==null)
        {
            messageInvalidDate("Zadávejte datum ve formátu den.měsíc.rok nebo použijte kalendář");
            return false;            
        }else{
            if(chooser.getDate().getTime()<0)
            {
                messageInvalidDate("Nesmyslné datum");
                return false;
            }else{
                return true;
            }
        }        
    }  
    
}